package com.icss.web.head.interceptor;

import java.util.Collections;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import com.icss.common.controller.BaseController;
import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.vo.ApiResponse;
import com.icss.common.vo.ApiResponseEnum;
import com.icss.users.vo.ReUsers;

/**
 * 	登陆验证的拦截器
 * @author 刘鑫
 */
@Component("authHeadInterceptor")
public class AuthHeadInterceptor extends BaseController implements HandlerInterceptor
{
	/**
	 * 先执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{
		ConstatFinalUtil.SYS_LOGGER.info("--授权拦截器执行--");
		/* 加载一些公共的变量 */
		this.commonOper(request);
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		HttpSession session = request.getSession() ; 
		/* 获取用户对象 */
		ReUsers users = (ReUsers) this.findObjfromSession(request, "users");
		if(users == null)
		{
			apiResponse.setCode(ApiResponseEnum.INFO_LOGIN_ILLEGAL.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			/* 提示信息 */
			session.setAttribute("response", apiResponse.toJSON());
			
			response.sendRedirect(request.getContextPath() + "/headLogin.htm");
			return false ;
		}
		return true;
	}
}
